Program
No. 1a
Implement a Java Servlet (SimpleServlet) that handles both HTTP GET and POST requests.
The servlet should:
For GET requests, respond with a simple HTML page that includes a form to submit a
name parameter.
For POST requests, retrieve username and password parameters from the request and
respond with an HTML page displaying these parameters. Include necessary servlet
(SimpleServlet.java )
package
com
.
example
;
import
java
.
io
.
IOException
;
import
javax
.
servlet
.
annotation
.
WebServlet
;
import
javax
.
servlet
.
http
.*;
@WebServlet
(
"/SimpleServlet"
)
public
class
SimpleServlet
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
response
.
setContentType
(
"text/html"
)
;
response
.
getWriter
()
.
println
(
"""
<html><body>
<h2>Submit Your Details</h2>
<form method='POST' action='SimpleServlet'>
Name: <input type='text' name='name'><br><br>
Password: <input type='password' name='password'><br><br>
<input type='submit' value='Submit'>
</form>
</body></html>
"""
)
;
}
@Override
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
String
name
=
request
.
getParameter
(
"name"
)
;
String
password
=
request
.
getParameter
(
"password"
)
;
response
.
setContentType
(
"text/html"
)
;
response
.
getWriter
()
.
println
(
"<html><body>"
+
"<h2>Submitted Data</h2>"
+
"Name: "
+
name
+
"<br>"
+
"Password: "
+
password
+
"<br>"
+
"</body></html>"
)
;
}
}
Output:
Program No. 1b
Implement a Java Servlet (SessionServlet) to demonstrate session tracking using
cookies and session objects.
The servlet should:
Display a login form when accessed initially.
Handle login functionality via a POST request, validating username and
password.
Upon successful login, store username in a session attribute and display a
welcome message with logout option.
Implement logout functionality to invalidate the session upon user request.
Display session information including session ID, creation time, and last
accessed time on the response page.
package
com
.
example
;
import
java
.
io
.
IOException
;
import
javax
.
servlet
.
annotation
.
WebServlet
;
import
javax
.
servlet
.
http
.*;
@WebServlet
(
"/ServletDemo/SessionServlet"
)
public
class
SessionServlet
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
response
.
setContentType
(
"text/html"
)
;
HttpSession
session
=
request
.
getSession
(
false
)
;
String
action
=
request
.
getParameter
(
"action"
)
;
if
(
"logout"
.
equals
(
action
)
&&
session
!=
null
)
{
session
.
invalidate
()
;
response
.
sendRedirect
(
"SessionServlet"
)
;
return
;
}
if
(
session
!=
null
&&
session
.
getAttribute
(
"username"
)
!=
null
)
{
response
.
getWriter
()
.
println
(
getSessionInfoPage
(
session
))
;
}
else
{
response
.
getWriter
()
.
println
(
getLoginFormPage
())
;
}
}
@Override
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
IOException
{
String
username
=
request
.
getParameter
(
"username"
)
;
String
password
=
request
.
getParameter
(
"password"
)
;
if
(
"admin"
.
equals
(
username
)
&&
"password"
.
equals
(
password
))
{
HttpSession
session
=
request
.
getSession
(
true
)
;
session
.
setAttribute
(
"username"
,
username
)
;
response
.
sendRedirect
(
"SessionServlet"
)
;
}
else
{
response
.
sendRedirect
(
"SessionServlet"
)
;
}
}
private
String
getSessionInfoPage
(
HttpSession
session
)
{
return
"<html><head><title>Welcome</title></head><body>"
+
"<h2>Welcome, "
+
session
.
getAttribute
(
"username"
)
+
"!</h2>"
+
"<p><a href='SessionServlet?action=logout'>Logout</a></p>"
+
"<h3>Session Information:</h3>"
+
"<p>Session ID: "
+
session
.
getId
()
+
"</p>"
+
"<p>Creation Time: "
+
new
java
.
util
.
Date
(
session
.
getCreationTime
())
+
"</p>"
+
"<p>Last
Accessed Time: "
+
new
java
.
util
.
Date
(
session
.
getLastAccessedTime
())
+
"</p>"
+
"</body></html>"
;
}
private
String
getLoginFormPage
()
{
return
"<html><head><title>Login</title></head><body>"
+
"<h2>Login</h2>"
+
"<form method='post' action='SessionServlet'>"
+
"Username: <input type='text' name='username'><br><br>"
+
"Password: <input type='password' name='password'><br><br>"
+
"<input type='submit' value='Login'>"
+
"</form>"
+
"</body></html>"
;
}
}
Output:
(username :admin ,password :password)
Program
No. 2a
Design a JSP application for managing employee information. Create a JSP
page (employeeForm.jsp) with an HTML form to collect employee data (e.g.,
Employee ID, Name, Age, Department, and Email) using the POST method.
Create a JSP page (employeeResult.jsp) to retrieve the submitted form data
using request.getParameter(). Use JSP expressions and scriptlets to process the
retrieved data. For example:
Concatenate the employee's name and department into a single string.
Calculate the birth year from the age.
Format the email address. Display the processed data in a formatted
manner in employeeResult.jsp.
//employeeForm.jsp
<!
DOCTYPE
html
>
<
html
>
<
head
>
<
title
>
Employee Form
</
title
>
</
head
>
<
body
>
<
h2
>
Employee Information Form
</
h2
>
<
form
action
=
"employeeResult.jsp"
method
=
"POST"
>
<
table
>
<
tr
>
<
td
>
Employee Id
</
td
>
<
td
><
input
type
=
"text"
name
=
"employeeId"
required
></
td
>
</
tr
>
<
tr
>
<
td
>
Name
</
td
>
<
td
><
input
type
=
"text"
name
=
"name"
required
></
td
>
</
tr
>
<
tr
>
<
td
>
Age
</
td
>
<
td
><
input
type
=
"number"
name
=
"age"
required
></
td
>
</
tr
>
<
tr
>
<
td
>
Department
</
td
>
<
td
><
input
type
=
"text"
name
=
"department"
required
></
td
>
</
tr
>
<
tr
>
<
td
>
Email
</
td
>
<
td
><
input
type
=
"email"
name
=
"email"
required
></
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
style
=
"
text-align
:
center
;
"
>
<
input
type
=
"submit"
value
=
"Submit"
>
</
td
>
</
tr
>
</
table
>
</
form
>
</
body
>
</
html
>
//employeeResult.jsp
<%@
page
import
=
"java.util.Calendar"
%>
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<!
DOCTYPE
html
>
<
html
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
title
>
Employee Information Result
</
title
>
</
head
>
<
body
>
<
h2
>
Employee Information Result
</
h2
>
<%
// Retrieving form data using request.getParameter()
String employeeId = request.getParameter(
"employeeId"
);
String name = request.getParameter(
"name"
);
String ageStr = request.getParameter(
"age"
);
String department = request.getParameter(
"department"
);
String email = request.getParameter(
"email"
);
// Processing the retrieved data
int
age = Integer.parseInt(ageStr);
Calendar calendar = Calendar.getInstance();
int
currentYear = calendar.get(Calendar.YEAR);
int
birthYear = currentYear - age;
String nameAndDepartment = name +
" ("
+ department
+
")"
;
String formattedEmail = email.toLowerCase();
%>
<
table
border
=
"1"
>
<
tr
>
<
th
>
Employee ID
</
th
>
<
td
>
<%=
employeeId
%>
</
td
>
</
tr
>
<
tr
>
<
th
>
Name and Department
</
th
>
<
td
>
<%=
nameAndDepartment
%>
</
td
>
</
tr
>
<
tr
>
<
th
>
Birth Year
</
th
>
<
td
>
<%=
birthYear
%>
</
td
>
</
tr
>
<
tr
>
<
th
>
Formatted Email
</
th
>
<
td
>
<%=
formattedEmail
%>
</
td
>
</
tr
>
</
table
>
</
body
>
</
html
>
Output:
Program
No. 2b
Enhance the employeeform.jsp page to implement robust server-side validation using JSP for
managing employee information. Ensure that required fields such as employee ID, name, age,
department, and email are properly validated in employeeresult.jsp upon form submission.
Implement the server-side checks to verify that:
Employee ID is a non-empty string.
Name contains only letters and spaces.
Age is a valid numeric value between 18 and 65.
Department is selected from a predefined list of options.
Email follows a valid format (e.g., user@gmail.com).
//employeeForm.jsp
<!
DOCTYPE
html
>
<
html
>
<
head
>
<
title
>
Employee Form
</
title
>
</
head
>
<
body
>
<
h2
>
Employee Information Form
</
h2
>
<
form
action
=
"employeeResult.jsp"
method
=
"POST"
>
<
table
>
<
tr
>
<
td
>
Employee Id
</
td
>
<
td
><
input
type
=
"text"
name
=
"empid"
required
></
td
>
</
tr
>
<
tr
>
<
td
>
Name
</
td
>
<
td
><
input
type
=
"text"
name
=
"name"
required
></
td
>
</
tr
>
<
tr
>
<
td
>
Age:
</
td
>
<
td
><
input
type
=
"number"
name
=
"age"
></
td
>
</
tr
>
<
tr
>
<
td
>
Department
</
td
>
<
td
>
<
select
name
=
"department"
required
>
<
option
value
=
""
disabled
selected
>
Select
your department
</
option
>
<
option
value
=
"HR"
>
HR
</
option
>
<
option
value
=
"Finance"
>
Finance
</
option
>
<
option
value
=
"IT"
>
IT
</
option
>
<
option
value
=
"Sales"
>
Sales
</
option
>
</
select
>
</
td
>
</
tr
>
<
tr
>
<
td
>
Email
</
td
>
<
td
><
input
type
=
"email"
name
=
"email"
></
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
style
=
"
text-align
:
center
;
"
>
<
input
type
=
"submit"
value
=
"Submit"
>
</
td
>
</
tr
>
</
table
>
</
form
>
</
body
>
</
html
>
//employeeResult.jsp
<%@
page
language
=
"java"
contentType
=
"text/html; charset=UTF-8"
pageEncoding
=
"UTF-8"
%>
<%@
page
import
=
"java.util.Calendar"
%>
<!
DOCTYPE
html
>
<
html
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
title
>
Employee Information
</
title
>
</
head
>
<
body
>
<
h2
>
Employee Information Result:
</
h2
>
<%
// Retrieve form data
String employeeid = request.getParameter(
"empid"
);
String name = request.getParameter(
"name"
);
String ageStr = request.getParameter(
"age"
);
String department = request.getParameter(
"department"
);
String email = request.getParameter(
"email"
);
String emailerror =
""
;
String employeeiderror =
""
;
String nameerror =
""
;
String departmenterror =
""
;
String ageerror =
""
;
boolean
isvalid =
true
;
if
(employeeid ==
null
|| employeeid.trim().isEmpty())
{
employeeiderror =
"Employee ID is required"
;
isvalid =
false
;
}
if
(name ==
null
|| !name.matches(
"^[a-zA-Z\\s]+$"
))
{
nameerror =
"Name should have only letters
and spaces"
;
isvalid =
false
;
}
int
age = 0;
try
{
age = Integer.parseInt(ageStr);
if
(age < 18 || age > 65) {
ageerror =
"Age must be between 18 and
65"
;
isvalid =
false
;
}
}
catch
(NumberFormatException e) {
ageerror =
"Age must be a valid numeric number"
;
isvalid =
false
;
}
if
(department ==
null
|| department.trim().isEmpty())
{
departmenterror =
"Department is required"
;
isvalid =
false
;
}
else
if
(!department.matches(
"HR|Finance|IT|Sales"
))
{
departmenterror =
"Department must be HR or
FINANCE or IT or Sales"
;
isvalid =
false
;
}
if
(email ==
null
|| !email.matches(
"^[\\w.-]+@[\\w.-]+\\.[a-zA-Z]{2,}$"
))
{
emailerror =
"Email ID is not valid"
;
isvalid =
false
;
}
String nameAndDepartment =
""
;
int
birthyear = 0;
String formattedEmail =
""
;
if
(isvalid) {
// Process data
Calendar calendar = Calendar.getInstance();
int
currentYear = calendar.get(Calendar.YEAR);
birthyear = currentYear - age;
nameAndDepartment = name +
" ("
+ department
+
")"
;
formattedEmail = email.toLowerCase();
%>
<
table
border
=
"1"
>
<
tr
>
<
th
>
Employee ID
</
th
>
<
td
>
<%=
employeeid
%>
</
td
>
</
tr
>
<
tr
>
<
th
>
Name and Department
</
th
>
<
td
>
<%=
nameAndDepartment
%>
</
td
>
</
tr
>
<
tr
>
<
th
>
Birth Year
</
th
>
<
td
>
<%=
birthyear
%>
</
td
>
</
tr
>
<
tr
>
<
th
>
Formatted Email ID
</
th
>
<
td
>
<%=
formattedEmail
%>
</
td
>
</
tr
>
</
table
>
<%
}
if
(!isvalid) {
%>
<
h3
>
Validated Errors:
</
h3
>
<
ul
>
<%
if
(!employeeiderror.isEmpty()) {
%>
<
li
>
<
b
>
Employee ID:
</
b
>
<%=
employeeiderror
%>
</
li
>
<%
}
%>
<%
if
(!nameerror.isEmpty()) {
%>
<
li
>
<
b
>
Name:
</
b
>
<%=
nameerror
%>
</
li
>
<%
}
%>
<%
if
(!ageerror.isEmpty()) {
%>
<
li
>
<
b
>
Age:
</
b
>
<%=
ageerror
%>
</
li
>
<%
}
%>
<%
if
(!departmenterror.isEmpty()) {
%>
<
li
>
<
b
>
Department:
</
b
>
<%=
departmenterror
%>
</
li
>
<%
}
%>
<%
if
(!emailerror.isEmpty()) {
%>
<
li
>
<
b
>
Email:
</
b
>
<%=
emailerror
%>
</
li
>
<%
}
%>
</
ul
>
<
a
href
=
"employeeForm.jsp"
>
Back to Login Page
</
a
>
<%
}
%>
</
body
>
</
html
>
Output :
(valid )
(Invalid )
Program
No. 3
Develop a JSP application that integrates JDBC to perform CRUD (Create, Read, Update, and
Delete) operations on a students database table. The application should:
Create: Insert new student records with fields such as:
Roll No
Name
Email
Age
Read: Retrieve and display all student records from the database.
Update: Modify existing student records.
Delete: Remove student records from the database.
Approach 1: (Original Code )
DBConnection.java
package
com
.
studentcrud
;
import
java
.
sql
.
Connection
;
import
java
.
sql
.
DriverManager
;
import
java
.
sql
.
SQLException
;
public
class
DBConnection
{
private
static
String
jdbcURL
=
"jdbc:mysql://localhost:3306/student"
;
private
static
String
jdbcUsername
=
"student"
;
private
static
String
jdbcPassword
=
"student"
;
public
static
Connection
getConnection
()
{
Connection
connection
=
null
;
try
{
Class
.
forName
(
"com.mysql.jdbc.Driver"
)
;
connection
=
DriverManager
.
getConnection
(
jdbcURL
,
jdbcUsername
,
jdbcPassword
)
;
}
catch
(
ClassNotFoundException
|
SQLException
e
)
{
e
.
printStackTrace
()
;
}
return
connection
;
}
}
Student.java
package
com
.
studentcrud
;
public
class
Student
{
private
int
rollNo
;
private
String
name
;
private
String
email
;
private
int
age
;
// Getters
public
int
getRollNo
()
{
return
rollNo
;
}
public
String
getName
()
{
return
name
;
}
public
String
getEmail
()
{
return
email
;
}
public
int
getAge
()
{
return
age
;
}
// Setters
public
void
setRollNo
(
int
rollNo
)
{
this
.
rollNo
=
rollNo
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
void
setAge
(
int
age
)
{
this
.
age
=
age
;
}
// toString method
@Override
public
String
toString
()
{
return
"Student{"
+
"rollNo="
+
rollNo
+
", name='"
+
name
+
'\''
+
", email='"
+
email
+
'\''
+
",
age="
+
age
+
'}'
;
}
}
StudentDAO.java
package
com
.
studentcrud
;
import
java
.
sql
.*;
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
public
class
StudentDAO
{
public
void
addStudent
(
Student
student
)
{
String
sql
=
"INSERT INTO students (roll_no,
name,email, age) VALUES (?, ?, ?, ?)"
;
try
(
Connection
connection
=
DBConnection
.
getConnection
()
;
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
sql
))
{
preparedStatement
.
setInt
(
1
,
student
.
getRollNo
())
;
preparedStatement
.
setString
(
2
,
student
.
getName
())
;
preparedStatement
.
setString
(
3
,
student
.
getEmail
())
;
preparedStatement
.
setInt
(
4
,
student
.
getAge
())
;
preparedStatement
.
executeUpdate
()
;
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
()
;
}
}
public
List
<
Student
>
getAllStudents
()
{
List
<
Student
>
students
=
new
ArrayList
<>
()
;
String
sql
=
"SELECT * FROM students"
;
try
(
Connection
connection
=
DBConnection
.
getConnection
()
;
Statement
statement
=
connection
.
createStatement
()
;
ResultSet
resultSet
=
statement
.
executeQuery
(
sql
))
{
while
(
resultSet
.
next
())
{
Student
student
=
new
Student
()
;
student
.
setRollNo
(
resultSet
.
getInt
(
"roll_no"
))
;
student
.
setName
(
resultSet
.
getString
(
"name"
))
;
student
.
setEmail
(
resultSet
.
getString
(
"email"
))
;
student
.
setAge
(
resultSet
.
getInt
(
"age"
))
;
students
.
add
(
student
)
;
}
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
()
;
}
return
students
;
}
public
void
updateStudent
(
Student
student
)
{
String
sql
=
"UPDATE students SET name = ?,
email = ?,age = ? WHERE roll_no = ?"
;
try
(
Connection
connection
=
DBConnection
.
getConnection
()
;
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
sql
))
{
preparedStatement
.
setString
(
1
,
student
.
getName
())
;
preparedStatement
.
setString
(
2
,
student
.
getEmail
())
;
preparedStatement
.
setInt
(
3
,
student
.
getAge
())
;
preparedStatement
.
setInt
(
4
,
student
.
getRollNo
())
;
preparedStatement
.
executeUpdate
()
;
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
()
;
}
}
public
void
deleteStudent
(
int
rollNo
)
{
String
sql
=
"DELETE FROM students WHERE roll_no
= ?"
;
try
(
Connection
connection
=
DBConnection
.
getConnection
()
;
PreparedStatement
preparedStatement
=
connection
.
prepareStatement
(
sql
))
{
preparedStatement
.
setInt
(
1
,
rollNo
)
;
preparedStatement
.
executeUpdate
()
;
}
catch
(
SQLException
e
)
{
e
.
printStackTrace
()
;
}
}
}
viewStudents.jsp
<%@
page
import
=
"java.util.List, com.studentcrud.Student,com.studentcrud.StudentDAO"
%>
<%
StudentDAO dao =
new
StudentDAO();
List<Student> students = dao.getAllStudents();
%>
<
html
>
<
body
>
<
h2
>
View Students
</
h2
>
<
table
border
=
"1"
>
<
tr
>
<
th
>
Roll No
</
th
>
<
th
>
Name
</
th
>
<
th
>
Email
</
th
>
<
th
>
Age
</
th
>
<
th
>
Actions
</
th
>
</
tr
>
<%
for
(Student student : students) {
%>
<
tr
>
<
td
>
<%=
student.getRollNo()
%>
</
td
>
<
td
>
<%=
student.getName()
%>
</
td
>
<
td
>
<%=
student.getEmail()
%>
</
td
>
<
td
>
<%=
student.getAge()
%>
</
td
>
<
td
>
<
a
href
=
"updateStudent.jsp?rollNo=
<%=
student.getRollNo()
%>
"
>
Edit
</
a
>
|
<
a
href
=
"deleteStudent.jsp?rollNo=
<%=
student.getRollNo()
%>
"
>
Delete
</
a
>
</
td
>
</
tr
>
<%
}
%>
</
table
>
<
a
href
=
"addStudent.jsp"
>
Add Student
</
a
>
</
body
>
</
html
>
addStudent.jsp
<%@
page
import
=
"com.studentcrud.Student,com.studentcrud.StudentDAO"
%>
<%
if
(request.getParameter(
"rollNo"
) !=
null
) {
int
rollNo =
Integer.parseInt(request.getParameter(
"rollNo"
));
String name = request.getParameter(
"name"
);
String email = request.getParameter(
"email"
);
int
age =Integer.parseInt(request.getParameter(
"age"
));
Student student =
new
Student();
student.setRollNo(rollNo);
student.setName(name);
student.setEmail(email);
student.setAge(age);
StudentDAO dao =
new
StudentDAO();
dao.addStudent(student);
out.println(
"Student added successfully."
);
}
%>
<
html
>
<
body
>
<
h2
>
Add Student
</
h2
>
<
form
action
=
"addStudent.jsp"
method
=
"post"
>
Roll No:
<
input
type
=
"text"
name
=
"rollNo"
><
br
>
Name:
<
input
type
=
"text"
name
=
"name"
><
br
>
Email:
<
input
type
=
"text"
name
=
"email"
><
br
>
Age:
<
input
type
=
"text"
name
=
"age"
><
br
>
<
input
type
=
"submit"
value
=
"Add Student"
>
</
form
>
<
a
href
=
"viewStudents.jsp"
>
View Students
</
a
>
</
body
>
</
html
>
deleteStudent.jsp
<%@
page
import
=
"com.studentcrud.StudentDAO"
%>
<%
int
rollNo = Integer.parseInt(request.getParameter(
"rollNo"
));
StudentDAO dao =
new
StudentDAO();
dao.deleteStudent(rollNo);
%>
<
html
>
<
body
>
<
p
>
Student deleted successfully.
</
p
>
<
a
href
=
"viewStudents.jsp"
>
View Students
</
a
>
</
body
>
</
html
>
updateStudent.jsp
%@ page import="com.studentcrud.Student,com.studentcrud.StudentDAO" %>
<%@
page
import
=
"com.studentcrud.Student"
%>
<%@
page
import
=
"com.studentcrud.StudentDAO"
%>
<%
StudentDAO
dao =
new
StudentDAO
();
int
rollNo =
Integer.parseInt(request.getParameter(
"rollNo"
));
Student
student =
new
Student
();
for
(
Student
s : dao.getAllStudents()) {
if
(s.getRollNo() == rollNo) {
student = s;
break
;
}
}
if
(request.getParameter(
"name"
) !=
null
) {
String name = request.getParameter(
"name"
);
String email = request.getParameter(
"email"
);
int
age =
Integer.parseInt(request.getParameter(
"age"
));
student.setName(name);
student.setEmail(email);
student.setAge(age);
dao.updateStudent(student);
out.println(
"Student updated successfully."
);
}
%>
<
html
>
<
body
>
<
h2
>
Update Student
</
h2
>
<
form
action
=
"updateStudent.jsp?rollNo=
<%=
student.getRollNo()
%>
"
method
=
"post"
>
Name:
<
input
type
=
"text"
name
=
"name"
value
=
"
<%=
student.getName()
%>
"
><
br
>
Email:
<
input
type
=
"text"
name
=
"email"
value
=
"
<%=
student.getEmail()
%>
"
><
br
>
Age:
<
input
type
=
"text"
name
=
"age"
value
=
"
<%=
student.getAge()
%>
"
><
br
>
<
input
type
=
"submit"
value
=
"Update Student"
>
</
form
>
<
a
href
=
"viewStudents.jsp"
>
View Students
</
a
>
</
body
></
html
>
Approach 2: (Optimized and Short Code )
DBConnection.java
package
com
.
studentcrud
;
import
java
.
sql
.*;
public
class
DBConnection
{
private
static
final
String
URL
=
"jdbc:mysql://localhost:3306/student"
,
USER
=
"root"
,
PASSWORD
=
"root"
;
public
static
Connection
getConnection
()
{
try
{
Class
.
forName
(
"com.mysql.jdbc.Driver"
)
;
return
DriverManager
.
getConnection
(
URL
,
USER
,
PASSWORD
)
;
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
)
;
// Handle
exceptions concisely
}
}
}
Student.java
package
com
.
studentcrud
;
public
class
Student
{
private
int
rollNo
;
private
String
name
;
private
String
email
;
private
int
age
;
// Getters
public
int
getRollNo
()
{
return
rollNo
;
}
public
String
getName
()
{
return
name
;
}
public
String
getEmail
()
{
return
email
;
}
public
int
getAge
()
{
return
age
;
}
// Setters
public
void
setRollNo
(
int
rollNo
)
{
this
.
rollNo
=
rollNo
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
void
setAge
(
int
age
)
{
this
.
age
=
age
;
}
// toString method
@Override
public
String
toString
()
{
return
"Student{"
+
"rollNo="
+
rollNo
+
", name='"
+
name
+
'\''
+
", email='"
+
email
+
'\''
+
", age="
+
age
+
'}'
;
}
}
StudentDAO.java
package com.studentcrud;
import
java.sql.*;
import
java.util.*;
public class
StudentDAO
{
public void
addStudent
(
Student
s
) throws
SQLException
{
try (
Connection
c
=
DBConnection
.getConnection();
PreparedStatement
p
=
c
.
prepareStatement
(
"INSERT
INTO students VALUES (?, ?,
?, ?)"
)) {
p
.
setInt
(
1
,
s
.getRollNo());
p
.
setString
(
2
,
s
.getName());
p
.
setString
(
3
,
s
.getEmail());
p
.
setInt
(
4
,
s
.getAge());
p
.
executeUpdate
();
}
}
public
List
<
Student
>
getAllStudents
() throws
SQLException
{
List
<
Student
>
list
= new
ArrayList
<>();
try (
Connection
c
=
DBConnection
.getConnection();
ResultSet
r
=
c
.
createStatement
().
executeQuery
(
"SELECT
* FROM students"
)) {
while (
r
.
next
()) {
Student
s
= new
Student
();
s
.setRollNo(
r
.
getInt
(
"roll_no"
));
s
.setName(
r
.
getString
(
"name"
));
s
.setEmail(
r
.
getString
(
"email"
));
s
.setAge(
r
.
getInt
(
"age"
));
list
.add(
s
);
}
}
return
list
;
}
public void
updateStudent
(
Student
s
) throws
SQLException
{
try (
Connection
c
=
DBConnection
.getConnection();
PreparedStatement
p
=
c
.
prepareStatement
(
"UPDATE
students SET name=?,
email=?, age=? WHERE roll_no=?"
)) {
p
.
setString
(
1
,
s
.getName());
p
.
setString
(
2
,
s
.getEmail());
p
.
setInt
(
3
,
s
.getAge());
p
.
setInt
(
4
,
s
.getRollNo());
p
.
executeUpdate
();
}
}
public void
deleteStudent
(int
rollNo
) throws
SQLException
{
try (
Connection
c
=
DBConnection
.getConnection();
PreparedStatement
p
=
c
.
prepareStatement
(
"DELETE
FROM students WHERE
roll_no=?"
)) {
p
.
setInt
(
1
,
rollNo
);
p
.
executeUpdate
();
}
}
}
viewStudents.jsp
<%@
page
import
=
"com.studentcrud.StudentDAO, java.util.List,
com.studentcrud.Student"
%>
<%
StudentDAO dao =
new
StudentDAO();
List<Student> students = dao.getAllStudents();
%>
<
html
>
<
body
>
<
h2
>
Student List
</
h2
>
<
table
border
=
"1"
>
<
tr
><
th
>
Roll
No
</
th
><
th
>
Name
</
th
><
th
>
Email
</
th
><
th
>
Age
</
th
><
th
>
Actions
</
th
></
tr
>
<%
for
(Student s : students) {
%>
<
tr
>
<
td
>
<%=
s.getRollNo()
%>
</
td
><
td
>
<%=
s.getName()
%>
</
td
>
<
td
>
<%=
s.getEmail()
%>
</
td
><
td
>
<%=
s.getAge()
%>
</
td
>
<
td
><
a
href
=
"updateStudent.jsp?rollNo=
<%=
s.getRollNo()
%>
"
>
Edit
</
a
>
|
<
a
href
=
"deleteStudent.jsp?rollNo=
<%=
s.getRollNo()
%>
"
>
Delete
</
a
></
td
>
</
tr
>
<%
}
%>
</
table
>
<
a
href
=
"addStudent.jsp"
>
Add Student
</
a
>
</
body
>
</
html
>
addStudent.jsp
<%@
page
import
=
"com.studentcrud.Student, com.studentcrud.StudentDAO"
%>
<%
if
(request.getParameter(
"rollNo"
) !=
null
) {
Student s =
new
Student();
s.setRollNo(Integer.parseInt(request.getParameter(
"rollNo"
)));
s.setName(request.getParameter(
"name"
));
s.setEmail(request.getParameter(
"email"
));
s.setAge(Integer.parseInt(request.getParameter(
"age"
)));
new
StudentDAO().addStudent(s);
out.print(
"Student added successfully."
);
}
%>
<
html
>
<
body
>
<
form
action
=
"addStudent.jsp"
method
=
"post"
>
Roll No:
<
input
type
=
"text"
name
=
"rollNo"
><
br
><
br
>
Name:
<
input
type
=
"text"
name
=
"name"
><
br
><
br
>
Email:
<
input
type
=
"text"
name
=
"email"
><
br
><
br
>
Age:
<
input
type
=
"text"
name
=
"age"
><
br
><
br
>
<
input
type
=
"submit"
value
=
"Add Student"
><
br
>
</
form
>
<
a
href
=
"viewStudents.jsp"
>
View Students
</
a
>
</
body
>
</
html
>
deleteStudent.jsp
<%@
page
import
=
"com.studentcrud.StudentDAO"
%>
<%
new
StudentDAO().deleteStudent(Integer.parseInt(request.getParameter(
"rollNo"
)));
out.print(
"Student deleted successfully."
);
%>
<
html
>
<
body
>
<
a
href
=
"viewStudents.jsp"
>
Back to List
</
a
>
</
body
>
</
html
>
updateStudent.jsp
<%@
page
import
=
"com.studentcrud.StudentDAO, com.studentcrud.Student"
%>
<%
StudentDAO dao =
new
StudentDAO();
int
rollNo =
Integer.parseInt(request.getParameter(
"rollNo"
));
Student student =
new
Student();
for
(Student s : dao.getAllStudents()) {
if
(s.getRollNo() == rollNo) {
student = s;
break
;
}
}
if
(request.getParameter(
"name"
) !=
null
) {
String name = request.getParameter(
"name"
);
String email = request.getParameter(
"email"
);
int
age =
Integer.parseInt(request.getParameter(
"age"
));
student.setName(name);
student.setEmail(email);
student.setAge(age);
dao.updateStudent(student);
out.println(
"Student updated successfully."
);
}
%>
<
html
>
<
body
>
<
form
action
=
"updateStudent.jsp?rollNo=
<%=
student.getRollNo()
%>
"
method
=
"post"
>
Name:
<
input
type
=
"text"
name
=
"name"
value
=
"
<%=
student.getName()
%>
"
><
br
>
Email:
<
input
type
=
"text"
name
=
"email"
value
=
"
<%=
student.getEmail()
%>
"
><
br
>
Age:
<
input
type
=
"text"
name
=
"age"
value
=
"
<%=
student.getAge()
%>
"
><
br
>
<
input
type
=
"submit"
value
=
"Update"
>
</
form
>
<
a
href
=
"viewStudents.jsp"
>
Back to List
</
a
>
</
body
>
</
html
>
Output:
(delete student)
(Terminal)
student@nmamit-ADL03-72:~/Desktop/newNNM22CS058$
mysql
-u student -p
Enter password: (
student)
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 8.0.34-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
use student;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> CREATE TABLE students (
-> roll_no INT NOT NULL PRIMARY KEY,
-> name VARCHAR(45) DEFAULT NULL,
-> email VARCHAR(45) DEFAULT NULL,
-> age INT DEFAULT NULL
-> );
mysql>
show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| students |
+-------------------+
1 row in set (0.00 sec)
mysql>
desc students;
+---------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| roll_no | int | NO | PRI | NULL | |
| name | varchar(100) | NO | | NULL | |
| email | varchar(100) | NO | UNI | NULL | |
| age | int | NO | | NULL | |
+---------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql>
select * from students;
+---------+--------+---------------------+-----+
| roll_no | name | email | age |
+---------+--------+---------------------+-----+
| 1| abcd| abcd@gmail.com | 20 |
+---------+--------+---------------------+-----+
2 rows in set (0.00 sec)
Program
No. 4
Develop a web application using Spring Boot to perform CRUD operations on Book information. The
application should use Thymeleaf for the view layer, Spring MVC for the controller layer, Spring Data JPA
and Hibernate for data access, and MySQL as the database. The application should manage book
attributes such as title, authors, edition, publication and price.
Project Structure:
Steps to Create a Spring Boot Starter Project
Go to File > New > Spring Starter Project
Enter project Name.
Choose Maven as the build tool and select the appropriate Java version.
Add required dependencies:
Spring Web
Spring Data JPA
Thymeleaf
MySQL Driver
Spring Boot DevTools
Click Finish to create the project.
Book.java
package
com
.
example
.
demo
;
import
jakarta
.
persistence
.
Entity
;
import
jakarta
.
persistence
.
GeneratedValue
;
import
jakarta
.
persistence
.
GenerationType
;
import
jakarta
.
persistence
.
Id
;
@
Entity
public
class
Book
{
@
Id
@
GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
Long
id
;
String
name
;
String
author
;
int
price
;
public
Book
()
{
//
TODO
Auto-generated constructor stub
}
public
Long
getId
()
{
return
id
;
}
public
void
setId
(
Long
id
)
{
this
.
id
=
id
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
String
getAuthor
()
{
return
author
;
}
public
void
setAuthor
(
String
author
)
{
this
.
author
=
author
;
}
public
int
getPrice
()
{
return
price
;
}
public
void
setPrice
(
int
price
)
{
this
.
price
=
price
;
}
}
BookRepository.java
package
com
.
example
.
demo
;
import
org
.
springframework
.
data
.
jpa
.
repository
.
JpaRepository
;
public
interface
BookRepository
extends
JpaRepository
<
Book
,
Long
>
{
}
BookManager.java
package
com
.
example
.
demo
;
import
java
.
util
.
List
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
stereotype
.
Service
;
@
Service
public
class
BookManager
{
@
Autowired
BookRepository
repository
;
public
List
<
Book
>
getAllBook
(){
return
repository
.
findAll
()
;
}
public
Book
getBookById
(
Long id
)
{
return
repository
.
findById
(
id
)
.
get
()
;
}
public
void
saveBook
(
Book
book
)
{
repository
.
save
(
book
)
;
}
public
void
deleteBook
(
Long id
)
{
repository
.
deleteById
(
id
)
;
}
}
AppController.java
package
com
.
example
.
demo
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
stereotype
.
Controller
;
import
org
.
springframework
.
ui
.
Model
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
GetMapping
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
ModelAttribute
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
PathVariable
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
PostMapping
;
@
Controller
public
class
AppController
{
@
Autowired
BookManager
manager
;
@
GetMapping
(
"/"
)
public
String
showHomePage
(
Model
model
)
{
model
.
addAttribute
(
"booklist"
,
manager
.
getAllBook
())
;
return
"index"
;
}
@
GetMapping
(
"/new"
)
public
String
showCreatePage
()
{
return
"create_book_form"
;
}
@
PostMapping
(
"/save"
)
public
String
saveBook
(
@
ModelAttribute
Book
book
)
{
manager
.
saveBook
(
book
)
;
return
"redirect:/"
;
}
@
GetMapping
(
"/edit/{id}"
)
public
String
editBook
(
@
PathVariable
Long
id
,
Model
model
)
{
model
.
addAttribute
(
"staff"
,
manager
.
getBookById
(
id
))
;
return
"edit_form"
;
}
@
GetMapping
(
"/delete/{id}"
)
public
String
deleteBook
(
@
PathVariable
Long
id
,
Model
model
)
{
manager
.
deleteBook
(
id
)
;
return
"redirect:/"
;
}
}
Index.html
<!
DOCTYPE
html
>
<
html
xmlns:th
=
"http://www.thymeleaf.org"
>
<
head
>
<
meta
charset
=
"ISO-8859-1"
>
<
title
>
Book List
</
title
>
</
head
>
<
body
>
<
div
align
=
"center"
>
<
h1
>
Book List
</
h1
>
<
br
/>
<
a
href
=
"/new"
>
Create New Book
</
a
>
<
table
border
=
"1"
cellpadding
=
"10"
cellspacing
=
"0"
>
<
tr
>
<
th
>
ID
</
th
>
<
th
>
Name
</
th
>
<
th
>
Author
</
th
>
<
th
>
Price
</
th
>
<
th
>
Action
</
th
>
</
tr
>
<
tr
th:each
=
"book:${booklist}"
>
<
td
th:text
=
"${book.id}"
/>
<
td
th:text
=
"${book.name}"
/>
<
td
th:text
=
"${book.author}"
/>
<
td
th:text
=
"${book.price}"
/>
<
td
>
<
a
th:href
=
"@{'/edit/' + ${book.id}}"
>
Edit
</
a
>
<
a
th:href
=
"@{'/delete/' + ${book.id}}"
>
Delete
</
a
>
</
td
>
</
tr
>
</
table
>
</
div
>
</
body
>
</
html
>
Create_book_form.html
<!
DOCTYPE
html
>
<
html
>
<
head
>
<
meta
charset
=
"ISO-8859-1"
>
<
title
>
Create New Staff
</
title
>
</
head
>
<
body
>
<
div
align
=
"center"
>
<
h1
>
Create New Book
</
h1
>
<
br
/>
<
form
action
=
"/save"
method
=
"post"
>
<
table
border
=
"0"
cellpadding
=
"10"
cellspacing
=
"10"
>
<
tr
>
<
td
>
Name
</
td
>
<
td
><
Input
type
=
"text"
name
=
"name"
/></
td
>
</
tr
>
<
tr
>
<
td
>
Author
</
td
>
<
td
><
Input
type
=
"text"
name
=
"author"
/></
td
>
</
tr
>
<
tr
>
<
td
>
Price
</
td
>
<
td
><
Input
type
=
"text"
name
=
"price"
/></
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
align
=
"center"
>
<
button
type
=
"submit"
>
Save
</
button
>
</
td
>
</
tr
>
</
table
>
</
form
>
</
div
>
</
body
>
</
html
>
edit_form.html
<!
DOCTYPE
html
>
<
html
xmlns:th
=
"http://www.thymeleaf.org"
>
<
head
>
<
meta
charset
=
"ISO-8859-1"
>
<
title
>
Edit Book Details
</
title
>
</
head
>
<
body
>
<
div
align
=
"center"
>
<
h1
>
Edit Book Details
</
h1
>
<
br
/>
<
form
action
=
"#"
th:action
=
"@{/save}"
th:object
=
"${staff}"
method
=
"post"
>
<
table
border
=
"0"
cellpadding
=
"10"
cellspacing
=
"0"
>
<
tr
>
<
td
>
ID
</
td
>
<
td
><
input
type
=
"text"
th:field
=
"*{id}"
readonly
=
"readonly"
/></
td
>
</
tr
>
<
tr
>
<
td
>
Name
</
td
>
<
td
><
input
type
=
"text"
th:field
=
"*{name}"
/></
td
>
</
tr
>
<
tr
>
<
td
>
Author
</
td
>
<
td
><
input
type
=
"text"
th:field
=
"*{author}"
/></
td
>
</
tr
>
<
tr
>
<
td
>
Price
</
td
>
<
td
><
input
type
=
"text"
th:field
=
"*{price}"
/></
td
>
</
tr
>
<
tr
>
<
td
colspan
=
"2"
align
=
"center"
>
<
button
type
=
"submit"
>
Save
</
button
>
</
td
>
</
tr
>
</
table
>
</
form
>
</
div
>
</
body
>
</
html
>
Update : application.properties
# Application name (optional)
spring.application.name=
pgm4
# Database connection settings (required)
spring.datasource.url=
jdbc:mysql://localhost:3306/student
spring.datasource.username=
root
spring.datasource.password=
root
# JPA properties for automatic schema management (optional - defaults to 'update')
spring.jpa.hibernate.ddl-auto=
update
Output:
(delete id=2)
(Terminal)
student@nmamit-ADL03-72:~/Desktop/newNNM22CS058$
mysql
-u student -p
Enter password: (
student)
mysql> use student;
Database changed
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| mobile |
| students |
+-------------------+
2 rows in set (0.00 sec)
mysql> CREATE TABLE book (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> name VARCHAR(50),
-> author VARCHAR(50),
-> price INT
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+-------------------+
| Tables_in_student |
+-------------------+
| book |
| mobile |
| students |
+-------------------+
3 rows in set (0.00 sec)
mysql> select * from book;
+----+-------------------------------+---------------+-------+
| id | name | author | price |
+----+-------------------------------+---------------+-------+
| 1 | The power of subconsious mind | Joseph Murphy | 149 |
+----+-------------------------------+---------------+-------+
1 row in set (0.00 sec)
Program
No. 5
Develop a Spring Boot application that provides RESTful APIs for performing CRUD operations
on Mobile information. The application should use Spring Data JPA and Hibernate for data
access to MySQL database that stores Mobile details such as model name, model number,
device operating system, manufacturer, and country of origin. JSON may be considered as the
return value of the API. Encapsulate error handling feature for situations such as record not
found. Demonstrate APIs working using curl command and Postman API testing tool.
dependencies:
1. Spring Web
2. Spring Data JPA
3. MySQL Driver
Mobile.java
package
com
.
example
.
demo
;
import
jakarta
.
persistence
.
Entity
;
import
jakarta
.
persistence
.
GeneratedValue
;
import
jakarta
.
persistence
.
GenerationType
;
import
jakarta
.
persistence
.
Id
;
@
Entity
public
class
Mobile
{
@
Id
@
GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
Long
id
;
String
name
;
String
model
;
String
ossystem
;
int
price
;
public
Mobile
()
{
//
TODO
Auto-generated constructor stub
}
public
Long
getId
()
{
return
id
;
}
public
void
setId
(
Long
id
)
{
this
.
id
=
id
;
}
public
String
getName
()
{
return
name
;
}
public
void
setName
(
String
name
)
{
this
.
name
=
name
;
}
public
String
getModel
()
{
return
model
;
}
public
void
setModel
(
String
model
)
{
this
.
model
=
model
;
}
public
String
getOssystem
()
{
return
ossystem
;
}
public
void
setOssystem
(
String
ossystem
)
{
this
.
ossystem
=
ossystem
;
}
public
int
getPrice
()
{
return
price
;
}
public
void
setPrice
(
int
price
)
{
this
.
price
=
price
;
}
}
MobileRepository.java
package
com
.
example
.
demo
;
import
org
.
springframework
.
data
.
jpa
.
repository
.
JpaRepository
;
public
interface
MobileRepository
extends
JpaRepository
<
Mobile
,
Long
>
{
}
MobileManager.java
package
com
.
example
.
demo
;
import
java
.
util
.
List
;
import
org
.
springframework
.
stereotype
.
Service
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
@
Service
public
class
MobileManager
{
@
Autowired
MobileRepository
repository
;
public
List
<
Mobile
>
getAllMobile
(){
return
repository
.
findAll
()
;
}
public
void
saveMobile
(
Mobile
b
)
{
repository
.
save
(
b
)
;
}
public
Mobile
getMobileById
(
Long id
)
{
return
repository
.
findById
(
id
)
.
get
()
;
}
public
void
deleteMobile
(
Long id
)
{
repository
.
deleteById
(
id
)
;
}
public
Boolean exists
(
Long id
)
{
return
repository
.
existsById
(
id
)
;
}
}
AppController.java
package
com
.
example
.
demo
;
import
java
.
util
.
List
;
import
java
.
util
.
NoSuchElementException
;
import
org
.
springframework
.
beans
.
factory
.
annotation
.
Autowired
;
import
org
.
springframework
.
http
.
HttpStatus
;
import
org
.
springframework
.
http
.
ResponseEntity
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
DeleteMapping
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
GetMapping
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
PathVariable
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
PostMapping
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
PutMapping
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
RequestBody
;
import
org
.
springframework
.
web
.
bind
.
annotation
.
RestController
;
@
RestController
public
class
AppController
{
@
Autowired
MobileManager
manager
;
@
GetMapping
(
"/mobile"
)
public
List
<
Mobile
>
listAllMobile
()
{
return
manager
.
getAllMobile
()
;
}
@
GetMapping
(
"/mobile/{id}"
)
public
ResponseEntity
<
Mobile
>
getMobile
(
@
PathVariable
Long id
)
{
try
{
Mobile
mobile
=
manager
.
getMobileById
(
id
)
;
return
new
ResponseEntity
<
Mobile
>
(
mobile
,
HttpStatus
.
OK
)
;
}
catch
(
NoSuchElementException e
)
{
return
new
ResponseEntity
<
Mobile
>
(
HttpStatus
.
NOT_FOUND
)
;
}
}
@
PostMapping
(
"/mobile"
)
public
void
saveMobile
(
@
RequestBody
Mobile
mobile
)
{
manager
.
saveMobile
(
mobile
)
;
}
@
PutMapping
(
"/mobile"
)
public
void
updateMobile
(
@
RequestBody
Mobile
mobile
)
{
manager
.
saveMobile
(
mobile
)
;
}
@
DeleteMapping
(
"/mobile/{id}"
)
public
ResponseEntity
<
Mobile
>
DeleteMobile
(
@
PathVariable
Long id
)
{
if
(
manager
.
exists
(
id
))
{
manager
.
deleteMobile
(
id
)
;
return
new
ResponseEntity
<
Mobile
>
(
HttpStatus
.
OK
)
;
}
else
return
new
ResponseEntity
<
Mobile
>
(
HttpStatus
.
NOT_FOUND
)
;
}
}
Update :application.properties
# Application name (optional)
spring.application.name=
pgm5
server.address=
0.0.0.0
# Database connection settings (required)
spring.datasource.url=
jdbc:mysql://localhost:3306/student
spring.datasource.username=
root
spring.datasource.password=
root
Commands:
Insert :
curl -X POST -H "Content-Type:application/json" -d
"{\"name\":\"phone2\",\"model\":\"a2\",\"ossystem\":\"os2\",\"price\":5000}"
http://localhost:8080/mobile
Display :
curl -X GET http://localhost:8080/mobile
Update :
curl -X PUT -H "Content-Type:application/json" -d
"{\"id\":1,\"name\":\"phone1\",\"model\":\"a90\",\"ossystem\":\"os90\",\"price\":9000}"
http://localhost:8080/mobile
Delete:
curl -X DELETE
http://localhost:8080/mobile/1
Output:
MySql Terminal :
student@nmamit-ADL03-72:~/Desktop/newNNM22CS058$
mysql
-u student -p
Enter password: (
student)
1)Add a New Mobile Phone to the Database and Display All Mobile
Data :
2)
Update:
update the price of the Samsung Galaxy
S21 to 999.
3)Delete:
delete the entries with id = 3 (OnePlus 9 Pro) and id = 4
(Google Pixel 6).
4)Display:
MySql Terminal :
Postman method:
1 POSTMAN.mp4
POSTMAN .mp4
https://www.postman.com/downloads/